package offer;

import java.util.ArrayList;
import java.util.List;

public class offerII_0037 {
    public int[] asteroidCollision(int[] asteroids) {
        List<Integer> list=new ArrayList<>();
        int num=asteroids.length;
        for(int i=0;i<num;++i){
            list.add(asteroids[i]);
        }
        while(same(list)){
            int len=list.size();
            int number=0;
            while(number<len-1){
                if(len<=1) break;
                if(list.get(number)>0&&list.get(number+1)<0){
                    if(Math.abs(list.get(number))==Math.abs(list.get(number+1))){
                        len-=2;
                        list.remove(number+1);
                        list.remove(number);
                    }
                    else{
                        len-=1;
                        if(Math.abs(list.get(number))<Math.abs(list.get(number+1))){
                            list.set(number,list.get(number+1));
                        }
                        else{
                            list.set(number,list.get(number));
                        }
                        list.remove(number+1);
                        number++;
                    }
                }
                else number++;
            }
        }
        int len=list.size();
        int[] result=new int[len];
        for(int i=0;i<len;++i){
            result[i]=list.get(i);
        }
        return result;
    }
    public boolean same(List<Integer> list){
        if(list.isEmpty()) return false;
        int len=list.size();
        if(len==1) return false;
        for(int i=0;i<len-1;++i){
            if(list.get(i)>0&&list.get(i+1)<0) return true;
        }
        return false;
    }
}
